package Func;

import Book.Book;
import Book.BookList;

import java.util.Scanner;

/**
* Created with IntelliJ IDEA.
* Description:
* User: 38917
* Date: 2025-02-16
* Time: 02:15
*/public class AddFunc implements Func{
    @Override
    public void work(BookList bookList) {
        Scanner in = new Scanner(System.in);
        System.out.println("请输入你要添加的图书");
        System.out.println("请输入书名");
        String name = in.nextLine();
        System.out.println("请输入作者");
        String author = in.nextLine();
        System.out.println("请输入价格");
        int price = in.nextInt();
        in.nextLine();//读入回车
        System.out.println("请输入类型");
        String type = in.nextLine();

        Book book = new Book(name,author,price,type,false);//实例化图书

        int curSize = bookList.getUsedSize();//获取当前书架中书的数量

        for (int i = 0; i < curSize; i++) {
            Book book1 = bookList.getBooks(i);
            if (name.equals(book1.getName())){      //name是String类型，重写了equals方法
                System.out.println("这本书已经存在,无法添加");
                return;
            }
        }
        System.out.println("添加成功");

        bookList.setBooks(curSize,book);

        bookList.setUsedSize(curSize + 1);
    }
}
